<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书-管理员列表</title>
    <link rel="icon" href="/static/images/t_logo.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/static/css/lyk-global.css" />
    <link rel="stylesheet" href="/static/elementui/lib/theme-chalk/index.css">
    <script src="/static/js/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/static/js/jquery.md5.js"></script>
    <script src="/static/js/requestApi.js" type="text/javascript"></script>
    <script src="/static/js/vue/vue.js"></script>
    <!-- 引入elementui -->
    <script src="/static/elementui/js/element.js"></script>
    <script src="/static/js/axios.min.js"></script>
    <script src="/static/js/axios-config.js"></script>
</head>
<style>
    [v-cloak] {
        display: none;
    }
</style>
<body style="background-color: #fff">
<div id="app" v-cloak>
    <div class="mt-8 ml-4">
        <div class="df-aic ">
            <el-input placeholder="请输入管理员名称" size="small" style="width:200px;" class="ml-2" v-model="queryParam.name" @keyup.native.enter="getTableDataMethod(true)"></el-input>
            <el-button type="primary" size="small" class="ml-2" @click="getTableDataMethod(true)">搜索</el-button>
            <el-button type="primary" size="small" class="ml-2" @click="showAddAdminDialogMethod(true)">新增</el-button>
        </div>
        <el-table :data="tableData" v-loading="tableDataLoading" class="mt-4">
            <el-table-column prop="userName" label="管理员账号"></el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-popconfirm :title='"是否删除该条数据？"'
                                   @confirm="deleteMemberMethod(scope.row)">
                        <el-button slot="reference" type="danger" size="small" v-loading="deleteLoading"  class="mt-2">删除</el-button>
                    </el-popconfirm>

                    <el-popconfirm :title='"重置后的密码为：8158787234aA"'
                                   @confirm="resetAdminPassword(scope.row)">
                        <el-button slot="reference" type="primary" size="small" v-loading="deleteLoading"  class="mt-2">重置密码</el-button>
                    </el-popconfirm>

                </template>
            </el-table-column>
        </el-table>
    </div>


    <el-pagination background layout="total,sizes,prev, pager, next" :total="tableDataPaging.total"
                   :page-sizes="[10, 20, 50]"
                   prev-text="上一页" next-text="下一页"
                   :page-size="tableDataPaging.pageSize"
                   :current-page="tableDataPaging.currentPage"
                   @current-change="tableDataPagingChange"
                   @prev-click="tableDataPagingChange" @next-click="tableDataPagingChange"
                   @size-change="tableDataPagingSizeChange"
                   style="float:right;margin-top: 40px;padding-bottom:50px;">
    </el-pagination>

    <el-dialog title="添加管理员" :visible.sync="showAddAdminDialogVisible" width="700px"  append-to-body	>
        <div>
            <div class="mt-8 ml-4">
                <el-form ref="addAdminForm" :model="addAdminForm" label-width="150px" >
                    <el-form-item label="账号：" prop="userName" required>
                        <el-input v-model="addAdminForm.userName" style="width:400px;"  size="small" placeholder="请输入账号"></el-input>
                    </el-form-item>
                    <el-form-item label="密码：" prop="password" required>
                        <el-input v-model="addAdminForm.password" style="width:400px;" placeholder="请输入密码" size="small"></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="df-aic-jcc mt-9 mb-9">
            <el-button size="small" type="primary" class="ml-6" @click="addAdminSubmitMethod">确定</el-button>
            <el-button size="small" class="ml-6" @click="showAddAdminDialogVisible = false">取消</el-button>
        </div>
    </el-dialog>

</div>
</body>

<script type="text/javascript">
    const vm = new Vue({ // 配置对象 options
        // 配置选项(option)
        el: '#app', // element: 指定用vue来管理页面中的哪个标签区域
        components: {
        },
        data: {
            queryParam : {
                name:"",
                idCard:""
            },
            tableData : [],
            tableDataLoading : false,
            tableDataPaging : {
                currentPage : 1,
                pageSize : 10,
                total : 0,
            },
            deleteLoading : false,
            addOrEditTypeDialogVisible : false,
            addOrEditTypeForm : {
                id : "",
                name : "",
                parentId : ""
            },
            showAddAdminDialogVisible:false,
            addAdminForm:{
                userName:"",
                password:""
            }
        },
        mounted() {
            this.$nextTick(function () {
                this.getTableDataMethod();

            });
        },
        methods: {
            tableDataPagingChange(val) {
                let that = this;
                that.tableDataPaging.currentPage = val;
                that.getTableDataMethod();
            },
            tableDataPagingSizeChange(val) {
                let that = this;
                that.tableDataPaging.pageSize = val;
                that.getTableDataMethod();
            },

            //获取表格的数据
            getTableDataMethod(isChongZhi){
                let that = this;
                if (isChongZhi) {
                    this.tableDataPaging.currentPage = 1;
                }
                const {name,idCard} = this.queryParam
                let queryData = {
                    userName: $.trim(name),
                    idCard: $.trim(idCard),
                    pageNum: this.tableDataPaging.currentPage,
                    pageSize: this.tableDataPaging.pageSize,
                }
                this.tableDataLoading = true;
                that.tableData = [];
                axios.post('/member/getAdminMemberList', queryData)
                    .then(result => {
                        let res = result.data;
                        this.tableDataLoading = false;
                        if (res.code === 0) {
                            that.tableData = res.data.list;
                            that.tableDataPaging.total = res.data.total;
                        }
                    }).catch(error => {
                    that.$message.error("error");
                    this.tableDataLoading = false
                })
            },

            jumpToEdiMemberMethod(row){
                window.open("addOrEditMember.html?id=" + row.id);
            },


            //删除
            deleteMemberMethod(row){
                let that = this;
                if(!row.id){
                    that.$message.error("尚无id");
                    return;
                };
                that.deleteLoading = true;
                requestApiTB("member/delMember",{"id" : row.id}, "POST", function(res){
                    that.deleteLoading = false;
                    if(res.code === 0){
                        that.$message.success("删除成功");
                        that.getTableDataMethod(true);
                    }else{
                        that.$message.error(res.msg);
                    }
                });
            },

            //重置密码
            resetAdminPassword(row){
                let that = this;
                if(!row.id){
                    that.$message.error("尚无id");
                    return;
                };
                let submitData = {
                    "id":row.id,
                    "password":$.md5("8158787234aA")
                }
                that.deleteLoading = true;
                requestApiTB("member/resetAdminPassword",submitData, "POST", function(res){
                    that.deleteLoading = false;
                    if(res.code === 0){
                        that.$message.success("重置成功");
                    }else{
                        that.$message.error(res.msg);
                    }
                });
            },

            //展示停驾管理员的弹窗
            showAddAdminDialogMethod(){
                let that = this;
                that.showAddAdminDialogVisible = true;
                this.$nextTick(function () {
                    that.$refs['addAdminForm'].resetFields();//重置字段
                });
            },

            addAdminSubmitMethod(){
                let that = this;
                let submitData = {
                    userName:that.addAdminForm.userName,
                    password:that.addAdminForm.password
                }
                if(!submitData.userName){
                    alert("请填写账号");
                    return;
                }
                if(!submitData.password){
                    alert("请填写密码");
                    return;
                }

                let regex = /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,20}$/;//校验密码规则
                if (!regex.test(submitData.password)) {
                    alert("密码必须是8-20位密码由大小写字母、数字组成");
                    return;
                }
                submitData.password = $.md5(submitData.password);
                requestApiTB("member/addAdmin",submitData, "POST", function(res){
                    if(res.code === 0){
                        alert("添加成功");
                        that.showAddAdminDialogVisible = false;
                        that.getTableDataMethod(true)
                    }else{
                        alert(res.msg);
                    }
                });

            },




        }
    })
</script>
</html>